﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using System.Web;
using Wuqi.Webdiyer;
using NLBS.Framework;
using System.Web.UI.WebControls;
using NLBS.Data.Business;
using NLBS.Web;

namespace NLBS.Data
{
    public class DataHelp
    {

        public static int CheckPageSize(AspNetPager pager)
        {
            int pageSize = pager.StartRecordIndex + pager.PageSize;
            if (pageSize > pager.RecordCount)
            {
                pageSize = pager.RecordCount - pager.StartRecordIndex + 1;
            }
            else
            {
                pageSize = pager.PageSize;
            }
            return pageSize;
        }

        public static void GetComment(AspNetPager pager, Repeater repeater, List<Comment> commentList)
        {
            pager.PageSize = BlogInfo.Instance().ListEntryPerPage;
            pager.EnableUrlRewriting = true;
            int userID = RequestWrapper.GetQueryInt("user");
            int catID = RequestWrapper.GetQueryInt("cat");
            string q = RequestWrapper.GetQueryString("q");
            if (!string.IsNullOrEmpty(q))
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.CommentCall('{0}','q','" + q + "')";
            } else
            if (userID > 0)
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.CommentCall('{0}','user'," + userID + ")";
            }
            else if (catID > 0)
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.CommentCall('{0}','cat'," + catID + ")";
            }
            else
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.CommentCall('{0}')";
            }
            pager.RecordCount = commentList.Count;
            int pageSize = CheckPageSize(pager);
            repeater.DataSource = commentList.GetRange(pager.StartRecordIndex - 1, pageSize);
            repeater.DataBind();
        }

        public static void GetGuestBook(AspNetPager pager, Repeater repeater, List<GuestBook> guestBookList)
        {
            pager.PageSize = BlogInfo.Instance().EntryPerPageGuestBook;
            pager.EnableUrlRewriting = true;
            string q = RequestWrapper.GetQueryString("q");
            if (!string.IsNullOrEmpty(q))
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.GuestBookCall('{0}','q','" + q + "')";
            }
            else
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.GuestBookCall('{0}')";
            }
            pager.RecordCount = guestBookList.Count;
            int pageSize = CheckPageSize(pager);
            repeater.DataSource = guestBookList.GetRange(pager.StartRecordIndex-1, pageSize);
            repeater.DataBind();
        }

        public static void GetTrackBack(AspNetPager pager, Repeater repeater, List<TrackBack> tbList)
        {
            pager.PageSize = BlogInfo.Instance().EntryPerPageGuestBook;
            pager.EnableUrlRewriting = true;
            int logID= RequestWrapper.GetQueryInt("id");
            int catID = RequestWrapper.GetQueryInt("cat");
            string q = RequestWrapper.GetQueryString("q");
            if (!string.IsNullOrEmpty(q))
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.TrackBackCall('{0}','q','" + q + "')";
            } else
            if (logID > 0)
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.TrackBackCall('{0}','id'," + logID + ")";
            }
            else if (catID > 0)
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.TrackBackCall('{0}','cat'," + catID + ")";
            }
            else
            {
                pager.UrlRewritePattern = "javascript:NLBSPage.TrackBackCall('{0}')";
            }
            pager.RecordCount = tbList.Count;
            int pageSize = CheckPageSize(pager);
            repeater.DataSource = tbList.GetRange(pager.StartRecordIndex - 1, pageSize);
            repeater.DataBind();
        }

        public static void GetUser(AspNetPager pager, Repeater repeater, List<User> userList)
        {
            pager.PageSize = 10;
            pager.EnableUrlRewriting = true;
            pager.UrlRewritePattern = "javascript:NLBSPage.UserCall('{0}')";
            pager.RecordCount = userList.Count;
            int pageSize = CheckPageSize(pager);
            repeater.DataSource = userList.GetRange(pager.StartRecordIndex - 1, pageSize);
            repeater.DataBind();
        }
    }
}
